package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb06Kc;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb06KcExc;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.KcNameTree;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.ZhhskcTree;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb06KcService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 矿产
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "矿产")
@RestController
@RequestMapping("/dictionary/cb06Kc")
@Validated
public class Cb06KcController {

    @Resource
    private ICb06KcService iCb06KcService;

    /**
     * 添加矿产
     */
    @ApiOperation("添加矿产")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb06Kc cb06Kc) {
        iCb06KcService.save(cb06Kc);
        return CommonResult.ok();
    }

    /**
     * 删除矿产
     */
    @ApiOperation("删除矿产")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb06KcService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑矿产
     */
    @ApiOperation("编辑矿产")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb06Kc cb06Kc) {
        iCb06KcService.updateById(cb06Kc);
        return CommonResult.ok();
    }
    /**
     * 矿产名称下拉框
     */
    @ApiOperation("矿产名称下拉框")
    @GetMapping("/getKcNameTree")
    public CommonResult<KcNameTree> getKcNameTree() {
        return CommonResult.data(iCb06KcService.getKcNameTree());
    }
    /**
     * 综合回收矿产名称下拉框
     */
//    @ApiOperation("综合回收矿产名称下拉框")
//    @GetMapping("/getZhhskcTree")
//    public CommonResult<ZhhskcTree> getZhhskcTree() {
//        return CommonResult.data(iCb06KcService.getZhhskcTree());
//    }

    /**
     * 综合回收矿产名称下拉框-重构
     *
     * @author Kevin
     * @since 2024/7/24 17:18
     */
    @ApiOperation("综合回收矿产名称下拉框-重构")
    @GetMapping("/getZhhskcTree")
    public CommonResult<ZhhskcTree> getZhhskcTreeList() {
        return CommonResult.data(iCb06KcService.getZhhskcTreeList());
    }

    /**
     * 矿产分页查询
     */
    @ApiOperation("矿产分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb06Kc>> page() {
        return CommonResult.data(iCb06KcService.page(new Cb06Kc()));
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb06KcExc> list = ExcelImportUtil.readMultipartFile(file, Cb06KcExc.class);
            List<Cb06Kc> cb01Zhs = GsonUtil.ObjectToList(list, Cb06Kc.class);
            iCb06KcService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
